package net.gz01.wallet.web.controller.admin;

import java.math.BigDecimal;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import net.gz01.util.ZODate;
import net.gz01.util.ZOMD5;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.RiskRule;
import net.gz01.wallet.web.model.TradeConfig;

public class RiskRuleController extends BaseController {

	public void search() {
		render("basedata/riskRuleList.html");
	}

	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<RiskRule> pageResult = RiskRule.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void addPage() {
		render("basedata/riskRuleAdd.html");
	}

	public void updatePage() {
		RiskRule risk = RiskRule.dao.findById(getPara("id"));
		setAttr("risk", risk);
		render("basedata/riskRuleUpdate.html");
	}

	@Before(Tx.class)
	public void add() {
		RiskRule risk = getModel(RiskRule.class, true);
		if (RiskRule.dao.selectByName(risk.getName()) != null) {
			renderFail("名称重复");
		} else {
			risk.setSumLimit(risk.getSumLimit() == null ? 0L : risk.getSumLimit() * 100);
			risk.setDayLimit(risk.getDayLimit() == null ? 0L : risk.getDayLimit() * 100);
			risk.setPayTypeDayLimit(risk.getPayTypeDayLimit() == null ? 0L : risk.getPayTypeDayLimit() * 100);
			risk.setMinSingleLimit(risk.getMinSingleLimit() == null ? 0 : risk.getMinSingleLimit() * 100);
			risk.setMaxSingleLimit(risk.getMaxSingleLimit() == null ? 0 : risk.getMaxSingleLimit() * 100);
			risk.setTradeStartTime(risk.getTradeStartTime() == null ? "" : risk.getTradeStartTime());
			risk.setTradeEndTime(risk.getTradeEndTime() == null ? "" : risk.getTradeEndTime());
			StringBuffer sb = new StringBuffer();
			sb.append("sumLimit" + risk.getSumLimit());
			sb.append("dayLimit" + risk.getDayLimit());
			sb.append("payTypeDayLimit" + risk.getPayTypeDayLimit());
			sb.append("minSingleLimit" + risk.getMinSingleLimit());
			sb.append("maxSingleLimit" + risk.getMaxSingleLimit());
			sb.append("tradeStartTime" + risk.getTradeStartTime());
			sb.append("tradeEndTime" + risk.getTradeEndTime());
			risk.setMd5(ZOMD5.codeFor(sb.toString()));
			risk.setUpdateTime(ZODate.getDateTime());
			risk.save();
			renderSuccess();
		}
	}

	@Before(Tx.class)
	public void update() {
		RiskRule risk = getModel(RiskRule.class, "risk");
		String sumLimitShow = getPara("sumLimitShow");
		BigDecimal sumLimit = new BigDecimal(sumLimitShow).multiply(new BigDecimal(100)); 
		String dayLimitShow = getPara("dayLimitShow");
		BigDecimal dayLimit = new BigDecimal(dayLimitShow).multiply(new BigDecimal(100)); 
		String payTypeDayLimitShow = getPara("payTypeDayLimitShow");
		BigDecimal payTypeDayLimit = new BigDecimal(payTypeDayLimitShow).multiply(new BigDecimal(100)); 
		String minSingleLimitShow = getPara("minSingleLimitShow");
		BigDecimal minSingleLimit = new BigDecimal(minSingleLimitShow).multiply(new BigDecimal(100)); 
		String maxSingleLimitShow = getPara("maxSingleLimitShow");
		BigDecimal maxSingleLimit = new BigDecimal(maxSingleLimitShow).multiply(new BigDecimal(100)); 
		
		risk.setSumLimit(sumLimit.longValue());
		risk.setDayLimit(dayLimit.longValue());
		risk.setPayTypeDayLimit(payTypeDayLimit.longValue());
		risk.setMinSingleLimit(minSingleLimit.intValue());
		risk.setMaxSingleLimit(maxSingleLimit.intValue());
		risk.setUpdateTime(ZODate.getDateTime());
		risk.update();
		renderSuccess();
	}

	@Before(Tx.class)
	public void delete() {
		if(TradeConfig.dao.isExistByRiskRuleId(getPara("id"))){
			String msg = "该规则已被引用，如需删除，请前往基础配置-交易配置更新交易规则！";
			renderSuccess(msg,false);
		}else {
			RiskRule r = RiskRule.dao.findById(getPara("id"));
			RiskRule.dao.deleteById(r.getId());
			renderSuccess(false);
		}
	}

}
